package com.yinhe.music.yhmusic.personal;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseActivity;
import com.yinhe.music.yhmusic.dialog.pay.PayDialog;
import com.yinhe.music.yhmusic.model.Music;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author 何晓华
 */
public class BuyMusicActivity extends BaseActivity {
    @BindView(R.id.btn_buy)
    Button btnBuy;
    @BindView(R.id.buy_layout)
    LinearLayout buyLayout;
    private Music music;
    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recent_play);
        unbinder = ButterKnife.bind(this);
        RxBus.get().register(this);
        if (getIntent() != null) {
            music = (Music) getIntent().getSerializableExtra("music");
        }
        initView();
        initBuyMusic();
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }

    private void initView() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction tx = fm.beginTransaction();
        Bundle bundle = new Bundle();
        setToolbar("购买单曲");
        BuyMusicFragment buyMusicFragment = new BuyMusicFragment();
        bundle.putSerializable("music", music);
        buyMusicFragment.setArguments(bundle);
        tx.add(R.id.frameLayout_buy, buyMusicFragment);
        tx.commit();
    }

    private void initBuyMusic() {
        btnBuy.setOnClickListener(view -> {
            PayDialog dialog = PayDialog.newInstance(String.valueOf(music.getSongId()));
            dialog.show(getSupportFragmentManager(), "PayDialog");
        });
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = @Tag(RxBusEventType.Music.BUY_MUSIC)
    )
    public void onRxbusBuyMusic(String id) {
        Toast.makeText(this, R.string.pay_success_tip, Toast.LENGTH_SHORT).show();
        finish();
    }

//    @Subscribe(
//            thread = EventThread.MAIN_THREAD,
//            tags = @Tag(RxBusEventType.Pay.PAY_STATUS)
//    )
//    public void onRxbusPaySuccess(Integer status) {
//        finish();
//    }
}
